<template>
  <div>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" :destroy-on-close="true" width="40%" >
      <el-form ref="form" :model="formData" label-width="100px" :rules="rules" size="small">
        <el-form-item label="菜单类型" prop="menuType">
            <el-radio-group v-model="formData.menuType" @input="formChange">
                <el-radio-button label="0" :disabled="directoryTypeDisabled">目录</el-radio-button>
                <el-radio-button label="1" :disabled="menuTypeDisabled">菜单</el-radio-button>
                <el-radio-button label="2" :disabled="buttonTypeDisabled">按钮</el-radio-button>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="菜单名称" prop="menuName">
            <el-input v-model="formData.menuName" placeholder="请输入"/>
        </el-form-item>
        <el-form-item >
            <el-input v-model="formData.permissionCode" placeholder="请输入" maxlength="100" />
            <span slot="label">
                <el-tooltip content="控制器中定义的权限字符，如：@PreAuthorize(hasAuthority('sys:user:list'))" placement="top">
                    <i class="el-icon-question"></i>
                </el-tooltip>
                权限标识
            </span>
        </el-form-item>
        <el-form-item prop="router" v-if="formData.menuType !== '2'">
          <span slot="label">
              <el-tooltip content="访问的路由地址，如：`/sysUser`" placement="top">
                  <i class="el-icon-question"></i>
              </el-tooltip>
              路由地址
          </span>
          <el-input v-model="formData.router" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="图标" prop="icon">
            <el-select v-model="formData.icon" style="width:100%" clearable>
                <el-option v-for="item in iconList" :key="item.class" :label="item.class" :value="item.class">
                    <span style="float: left;">
                        <i :class="item.class"></i>  <!-- 如果动态显示图标，这里添加判断 -->
                    </span>
                    <span style="padding-left: 6px;">{{ item.class }}</span>
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
            <el-input-number v-model="formData.sort" controls-position="right" :min="0" style="width:100%"/>
        </el-form-item>
        <el-form-item label="是否显示" prop="showFlag">
            <el-radio-group v-model="formData.showFlag">
                <el-radio-button label="1" :disabled="yesShowDisabled">是</el-radio-button>
                <el-radio-button label="0" :disabled="noShowDisabled">否</el-radio-button>
            </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" :loading="loading" @click="save()">保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

import { getMenu, saveMenu } from '@/api/sys/menu'

export default {
  name: 'dictForm',
  data() {
    return {
      loading: false,
      directoryTypeDisabled: false,
      menuTypeDisabled: false,
      buttonTypeDisabled: false,
      yesShowDisabled: false,
      noShowDisabled: false,
      title: '',
      dialogVisible: false,
      // 编辑与添加
      formData: {
        id: '',
        menuName: '',
        parentId: '',
        menuType: '0',
        permissionCode: '',
        icon: '',
        router: '',
        sort: 0,
        showFlag: '1'
      },
      defaultMenuType: '',
      iconList: [
        {
          class: "el-icon-s-tools",
        },
        {
          class: "el-icon-s-custom",
        },
        {
          class: "el-icon-setting",
        },
        {
          class: "el-icon-user-solid",
        },
        {
          class: "el-icon-s-help",
        },
        {
          class: "el-icon-phone",
        },
        {
          class: "el-icon-s-unfold",
        },
        {
          class: "el-icon-s-operation",
        },
        {
          class: "el-icon-more-outline",
        },
        {
          class: "el-icon-s-check",
        },
        {
          class: "el-icon-tickets",
        },
        {
          class: "el-icon-s-goods",
        },
        {
          class: "el-icon-document-remove",
        },
        {
          class: "el-icon-warning",
        },
        {
          class: "el-icon-warning-outline",
        },
        {
          class: "el-icon-question",
        },
        {
          class: "el-icon-info",
        }
      ],
      // rules表单验证
      rules: {
        menuName: [
          { required: true, message: '请输入菜单名称', trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    formChange() {
      this.formData.menuName = ''
      this.formData.permissionCode = ''
      this.formData.icon = ''
      this.formData.router = ''
      this.formData.sort = 0
      if (this.formData.menuType === '2') {
        this.formData.showFlag = '0'
        this.yesShowDisabled = true
        this.noShowDisabled = false
      } else {
        this.formData.showFlag = '1'
        this.yesShowDisabled = false
        this.noShowDisabled = false
      }
    },
    create() {
      this.title = '添加目录'
      this.dialogVisible = true
      this.directoryTypeDisabled = false
      this.menuTypeDisabled = true
      this.buttonTypeDisabled = true
      this.yesShowDisabled = false
      this.noShowDisabled = false
      this.formData = {
        id: '',
        menuName: '',
        parentId: '',
        menuType: '0',
        permissionCode: '',
        icon: '',
        router: '',
        sort: 0,
        showFlag: '1'
      }
    },
    createChildren(row) {
      this.title = '上级菜单：' + row.menuName
      this.dialogVisible = true
      this.directoryTypeDisabled = false
      this.menuTypeDisabled = false
      this.buttonTypeDisabled = false
      this.yesShowDisabled = false
      this.noShowDisabled = false
      if (row.menuType === '0') {
        this.defaultMenuType = '0'
      }
      var showFlag = '1'
      if (row.menuType === '1') {
        this.defaultMenuType = '2'
        this.directoryTypeDisabled = true
        this.menuTypeDisabled = true
        this.buttonTypeDisabled = false
        showFlag = '0'
        this.yesShowDisabled = true
        this.noShowDisabled = false
      }
      this.formData = {
        id: '',
        menuName: '',
        parentId: row.id,
        menuType: this.defaultMenuType,
        permissionCode: '',
        icon: '',
        router: '',
        sort: 0,
        showFlag: showFlag
      }
    },
    update(row) {
      getMenu(row.id).then(res => {
          if (res.status === 200) {
              this.title = '编辑菜单'
              this.dialogVisible = true
              this.directoryTypeDisabled = true
              this.menuTypeDisabled = true
              this.buttonTypeDisabled = true
              this.yesShowDisabled = false
              this.noShowDisabled = false
              if (row.menuType === '0') {
                this.directoryTypeDisabled = false
              }
              if (row.menuType === '1') {
                this.menuTypeDisabled = false
              }
              if (row.menuType === '2') {
                this.buttonTypeDisabled = false
                if (row.showFlag === '1') {
                  this.yesShowDisabled = false
                  this.noShowDisabled = true
                } 
                if (row.showFlag === '0') {
                  this.yesShowDisabled = true
                  this.noShowDisabled = false
                }
              }
              this.formData = res.data
          }
      })
    },
    save() {
      this.$refs.form.validate(valid => {
        if (valid) {
          // 请求方法
          this.loading = true
          saveMenu(this.formData).then(res => {
              if (res.status === 200) {
                this.dialogVisible = false
                this.loading = false
                this.$emit('saveAfter')
                this.$message({
                  type: 'success',
                  message: '保存成功！'
                })
              }
            })
            .catch(err => {
              this.dialogVisible = false
              this.loading = false
              this.$message.error('保存失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 